package com.zp.framework.services.test;

import javax.ws.rs.core.MediaType;

import junit.framework.Assert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.paper.ResultAnswerResultVo;
import com.zp.bean.paper.ResultPaperVo;
import com.zp.domain.AnswerResult;
import com.zp.util.Constant;

/**
 * test Answer result services function with Junit
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/2/23
 * 
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:configure/applicationContext.xml" })
public class RunAnswerResultServices {

	private static final Log logger = LogFactory
			.getLog(RunAnswerResultServices.class);
	private static final String URI = "http://localhost:8082/ZP";
	private WebResource service;

	@Before
	public void init() {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		service = client.resource(URI);
		logger.info("*********** testing to initial services client ****************");
	}

	@After
	public void destory() {
		service = null;
		logger.info("*********** testing to destory services instance ******************");
	}

	/** @Test **/
	public void testAddAnswerResult() {
		try {
			AnswerResult anresult = new AnswerResult();
			anresult.setPaperId("402881e6445c883e01445c8a74290001");
			anresult.setQuestionId("402881e6445cc3a301445cc4e73b0003");
			anresult.setAnswerItemId("402881e6445d53b401445d584d7c000b");
			anresult.setAuthor("402881e64459ad0e014459adc3b00001");
			anresult.setStatus("1");
			ClientResponse response = service.path("anresult/addAnResult")
					.type(MediaType.APPLICATION_JSON_TYPE)
					.post(ClientResponse.class, anresult);
			ResultCommonVo result = response.getEntity(ResultCommonVo.class);
			Assert.assertEquals(Constant.OK, result.getMessage());
		} catch (UniformInterfaceException e) {
			System.out.println(e.getResponse().getStatus());
			if (e.getResponse().getStatus() == 404) {
			} else if (e.getResponse().getStatus() == 401) {
				throw new IllegalStateException(e);
			} else if (e.getResponse().getStatus() == 403) {
				throw new IllegalStateException(e);
			} else {
				throw e;
			}
		}

	}

	/** @Test **/
	public void testUpdateAnResult() {
		AnswerResult anresult = new AnswerResult();
		ClientResponse response = service.path("anresult/updateAnResult")
				.type(MediaType.APPLICATION_JSON_TYPE)
				.put(ClientResponse.class, anresult);
		Assert.assertNotNull(response.getEntity(ResultCommonVo.class));

	}

	/** @Test **/
	public void testGetPapersForUser() {
		ClientResponse response = service.path("anresult/getPapersForUser")
				.queryParam("pageNo", "1").queryParam("pageSize", "2")
				.queryParam("userId", "402881e64459ad0e014459adc3b00001")
				.type(MediaType.APPLICATION_JSON_TYPE)
				.get(ClientResponse.class);
		ResultPaperVo result = response.getEntity(ResultPaperVo.class);
		Assert.assertEquals(1, result.getTotalCount());
	}

	/** @Test **/
	public void testGetResultsForUser() {
		ClientResponse response = service.path("anresult/getResultsForUser")
				.queryParam("pageNo", "1").queryParam("pageSize", "2")
				.queryParam("userId", "402881e64459ad0e014459adc3b00001")
				.queryParam("paperId", "402881e6445c883e01445c8a74290001")
				.type(MediaType.APPLICATION_JSON_TYPE)
				.get(ClientResponse.class);
		ResultAnswerResultVo result = response
				.getEntity(ResultAnswerResultVo.class);
		Assert.assertEquals(3, result.getTotalCount());
	}

	@Test
	public void testGetScore() {
		ClientResponse response = service.path("anresult/getScore")
				.queryParam("userId", "402881e64459ad0e014459adc3b00001")
				.queryParam("paperId", "402881e6445c883e01445c8a74290001")
				.type(MediaType.APPLICATION_JSON_TYPE)
				.get(ClientResponse.class);
		Assert.assertEquals("66",response.getEntity(ResultCommonVo.class).getMessage());

	}
}
